package com.cdu.controller;

import com.cdu.commons.MallConstants;
import com.cdu.commons.R;
import com.cdu.pojo.entity.Order;
import com.cdu.pojo.entity.OrderItem;
import com.cdu.pojo.entity.User;
import com.cdu.service.OrderService;
import com.cdu.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @projectName: com.cdu.controller
 * @Author:jjj
 * @DateTime: 2024/12/18 下午8:18
 **/
@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     */
    @PostMapping("createOrder")
    public R<Void> createOrder(Order order, OrderItem orderItem, HttpServletRequest request){
        // 从请求中获取身份
        String token = request.getHeader(MallConstants.TOKEN_SIGN);
        User user = null;
        if (StringUtils.hasLength(token)) {
            // 解析token
            user = JwtUtils.parseToken(token);
        }
        orderService.createOrder(order, user);
        orderService.createOrderItem(orderItem,user);
        return R.ok("订单创建成功");
    }

    /**
     * 订单支付
     */

}
